package greedy;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/1 0:06
 * https://leetcode-cn.com/problems/monotone-increasing-digits/
 */
public class MonotoneIncreasingDigits {

    public static int monotoneIncreasingDigits(int n) {
        char[] s = String.valueOf(n).toCharArray();
        int start = s.length;
        for (int i = s.length - 1; i > 0; i--) {
            if (s[i] < s[i - 1]) {
                s[i - 1] -= 1;
                start = i;
            }
        }
        int sum = 0;
        for (int i = 0; i < s.length; i++) {
            if (i >= start) {
                s[i] = '9';
            }
            sum = 10 * sum + (s[i] - '0');
        }
        return sum;
    }

    public static void main(String[] args) {
        System.out.println(monotoneIncreasingDigits(100));
    }
}
